﻿using System.Linq;
using System.Web.Mvc;
using CID.Domain.Infrastructure;
using CID.Web.Controllers;
using CID.Web.Models.Infrastructure;
using NailsFramework.IoC;
using NailsFramework.Persistence;
using NailsFramework.UserInterface.TestingSupport;
using NUnit.Framework;
using Rhino.Mocks;

namespace CID.Web.Tests.Controllers
{
    [TestFixture(Description = "This class test the functionality for creating template copies")]
    class TemplateControllerTests : BaseWebTests
    {
        [Inject]
        public IBag<Template> Entities { get; set; }

        private TemplateController controller;

        public override void SetUp()
        {
            base.SetUp();

            controller = TestControllers.Get<TemplateController>();
            var requestContext = StubsBuilder.BuildRequestContextStub();
            controller.ControllerContext = new ControllerContext(requestContext, controller);
            controller.Url = new UrlHelper(requestContext);
        }

        [Test]
        public void GetCreateCopy_ShouldCreateCreateViewWithNewEntity()
        {
            var result = controller.CreateCopy(InfrastructureData.ActivationTemplate.Id);
            var resultModel = result.Model as TemplateViewModel;

            Assert.IsNotNull(result);
            Assert.AreEqual("Create", result.ViewName);
            Assert.IsInstanceOf<TemplateViewModel>(result.Model);
            Assert.AreEqual(0, resultModel.Id);
            Assert.IsTrue(string.IsNullOrEmpty(resultModel.Name));
            Assert.IsTrue(string.IsNullOrEmpty(resultModel.Description));
        }

        [Test]
        public void PostCreate_ValidDataForActivation_ShouldCreateEntityAndRedirectToIndex()
        {
            var originalEntitiesCount = Entities.Count();
            var viewModel = new TemplateViewModel(InfrastructureData.ActivationTemplate) { Id = 0, Name = "Testing Creation" };
            foreach (var parameter in viewModel.Parameters) parameter.Id = 0;

            var result = controller.Create(viewModel);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf<RedirectToRouteResult>(result);
            Assert.AreEqual("Index", ((RedirectToRouteResult)result).RouteValues["action"]);
            Assert.AreEqual(originalEntitiesCount + 1, Entities.Count());
            var model = Entities.Skip(originalEntitiesCount).FirstOrDefault();
            Assert.IsInstanceOf<ActivationTemplate>(model);
        }

        [Test]
        public void PostCreate_ValidDataForDeactivation_ShouldCreateEntityAndRedirectToIndex()
        {
            var originalEntitiesCount = Entities.Count();
            var viewModel = new TemplateViewModel(InfrastructureData.DeactivationTemplate) { Id = 0, Name = "Testing Creation" };
            foreach (var parameter in viewModel.Parameters) parameter.Id = 0;

            var result = controller.Create(viewModel);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf<RedirectToRouteResult>(result);
            Assert.AreEqual("Index", ((RedirectToRouteResult)result).RouteValues["action"]);
            Assert.AreEqual(originalEntitiesCount + 1, Entities.Count());
            var model = Entities.Skip(originalEntitiesCount).FirstOrDefault();
            Assert.IsInstanceOf<DeactivationTemplate>(model);
        }

        [Test]
        public void PostCreate_ValidDataForDeployment_ShouldCreateEntityAndRedirectToIndex()
        {
            var originalEntitiesCount = Entities.Count();
            var viewModel = new TemplateViewModel(InfrastructureData.DeploymentTemplate) { Id = 0, Name = "Testing Creation" };
            foreach (var parameter in viewModel.Parameters) parameter.Id = 0;

            var result = controller.Create(viewModel);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf<RedirectToRouteResult>(result);
            Assert.AreEqual("Index", ((RedirectToRouteResult)result).RouteValues["action"]);
            Assert.AreEqual(originalEntitiesCount + 1, Entities.Count());
            var model = Entities.Skip(originalEntitiesCount).FirstOrDefault();
            Assert.IsInstanceOf<DeploymentTemplate>(model);
        }
    }
}
